package com.ygqh.baby.controller.os;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ygqh.baby.po.BiOrderDay;
import com.ygqh.baby.po.YgProductSecond;
import com.ygqh.baby.service.impl.DocumentConstant;
import com.ygqh.baby.service.task.YgBiTaskService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.controller.mobile.BaseController;
import com.ygqh.baby.model.YgUserStatisticsModel;
import com.ygqh.baby.service.YgProductSecondService;
import com.ygqh.baby.service.YgUserService;
import com.ygqh.baby.utils.DateConvertUtils;

@Controller
@RequestMapping("/os/statistics")
public class YgStatisticsController extends BaseController {
	@Autowired
	private YgUserService ygUserService;
	@Autowired
	private YgProductSecondService ygProductSecondService;
	@Autowired
	private YgBiTaskService ygBiTaskService;
	@Autowired
	private DocumentConstant documentConstant;

	@RequestMapping("findUserStatistics")
	@ResponseBody
	public JSONPObject findUserStatistics(String sourceCode, String callback) {
		Assert.notNull(sourceCode);
		JSONPObject json = null;
		List<YgUserStatisticsModel> list = ygUserService.findUserGrouBySourceCode(sourceCode);
		json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, list));
		return json;
	}

	@RequestMapping("findUserStatisticsLM")
	@ResponseBody
	public JSONPObject findUserStatisticsLM(String sourceCode, Date startDate, Date endDate, String callback) {
		JSONPObject json = null;
		List<Map<String, Object>> list = ygUserService.findUserStatisticsLM(startDate, endDate, sourceCode);
		json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, list));
		return json;
	}

	@RequestMapping("statisticsByTimeForM")
	@ResponseBody
	public JSONPObject statisticsByTimeForM(String[] sourceCodes, Date startDate, Date endDate, String callback) {

		List<BiOrderDay> list = new ArrayList<BiOrderDay>();
		long dayes = DateConvertUtils.diffDayes(startDate, endDate);
		for (int i = 0; i < dayes; i++) {
			Date date = DateConvertUtils.addDay(startDate, i);
			if (date.before(endDate)) {
				BiOrderDay orderDay = ygBiTaskService.statisticsByTimeForM(date, DateConvertUtils.addDay(date, 1), sourceCodes);
				list.add(orderDay);
			}
		}
		return new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, list));
	}

	@RequestMapping("go")
	@ResponseBody
	public JSONPObject go(String callback) {
		YgProductSecond second = ygProductSecondService.findActive();
		Map<String, Object> map = new HashMap<String, Object>();
		if (second != null) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

			map.put("endTime", second.getEndTime());
			map.put("startTime", second.getStartTime());
			map.put("currTime", df.format(new Date()));

			// 比较当前时间是否大于startTime ,大于则开始，

			map.put("isStart", second.getStartTime().after(new Date()) ? "0" : "1");
			// 比较当前时间是否大于endTime ,大于则结束，
			map.put("isEnd", second.getEndTime().after(new Date()) ? "0" : "1");
		} else {
			map.put("isStart", 0);
			map.put("isEnd", 1);
		}
		JSONPObject json = null;
		json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, map));
		return json;
	}
}
